﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for PomplingCustomers
/// </summary>
public class PomplingCustomers
{
    #region properties

    // Sql Type: int
    public string PomplingCustomerID { get; set; }

    // Sql Type: varchar
    public string UserName { get; set; }

    // Sql Type: varchar
    public string Email { get; set; }

    // Sql Type: varchar
    public string PomplingPassword { get; set; }

    // Sql Type: bit
    public bool IsCompany { get; set; }

    // Sql Type: smalldatetime
    public DateTime DateCustomerAdded { get; set; }

   

    #endregion properties

    public bool Save()
    {        
        //int len = PomplingCustomerID.Length;
        using (SqlConnection conn = new SqlConnection(Globals.GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("PomPomplingCustomerSave", conn))
            {
                this.IsCompany = false;
                try
                {
                    if (string.IsNullOrWhiteSpace(PomplingCustomerID))
                    {
                        Guid messageId = System.Guid.NewGuid();
                        PomplingCustomerID = messageId.ToString().Replace("-", string.Empty);
                    }
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@pPomplingCustomerID", this.PomplingCustomerID);
                    cmd.Parameters.AddWithValue("@pUsername", this.UserName);
                    cmd.Parameters.AddWithValue("@pEmail", this.Email);
                    cmd.Parameters.AddWithValue("@pPomplingPassword", this.PomplingPassword);
                    cmd.Parameters.AddWithValue("@pIsCompany", this.IsCompany);

                    cmd.Connection.Open();
                    object o = cmd.ExecuteScalar();                    
                    this.PomplingCustomerID = o.ToString();
                    cmd.Connection.Close();
                }
                catch (Exception)
                {
                    PomplingCustomerID = string.Empty;
                    throw;
                }
            }
        }
        return true;
    }

    public static bool UsernameUsed(string username)
    {
        PomplingCustomers customer = CustomerSelectByUsername(username);
        string customerID = (string.IsNullOrWhiteSpace(customer.PomplingCustomerID))?string.Empty:customer.PomplingCustomerID; 

        return (customerID.Length > 0); 
    }

    public static PomplingCustomers CustomerSelectByUsername(string username)
    {
        using (SqlConnection conn = new SqlConnection(Globals.GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("PomCustomerUsernameUsed", conn))
            {
                try
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@pUsername", username);
                    cmd.Connection.Open();
                    IDataReader reader = cmd.ExecuteReader();
                    DataTable dt = new DataTable("temp");
                    dt.Load(reader);
                    cmd.Connection.Close();

                    List<PomplingCustomers> customers = Artizan.Utility.FieldMapper.DataTableToList<PomplingCustomers>(ref dt);
                    if (customers.Count > 0)
                    {
                        return customers[0];
                    }

                    return new PomplingCustomers();

                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }

    public static bool EmailUsed(string email)
    {
        using (SqlConnection conn = new SqlConnection(Globals.GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("PomCustomerEmailUsed", conn))
            {
                try
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@pCustomerEmail", email);
                    cmd.Connection.Open();
                    IDataReader reader = cmd.ExecuteReader();
                    DataTable dt = new DataTable("temp");
                    dt.Load(reader);
                    cmd.Connection.Close();

                    List<PomplingCustomers> customers = Artizan.Utility.FieldMapper.DataTableToList<PomplingCustomers>(ref dt);
                    return (customers.Count > 0);

                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }

    public static PomplingCustomers CustomerSelect(string customerID)
    {
        using (SqlConnection conn = new SqlConnection(Globals.GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("PomCustomerSelect", conn))
            {
                try
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@pPomplingCustomerID", customerID);
                    cmd.Connection.Open();
                    IDataReader reader = cmd.ExecuteReader();
                    DataTable dt = new DataTable("temp");
                    dt.Load(reader);
                    cmd.Connection.Close();

                    List<PomplingCustomers> customers = Artizan.Utility.FieldMapper.DataTableToList<PomplingCustomers>(ref dt);
                    if (customers.Count > 0)
                        return customers[0];
                    return new PomplingCustomers();

                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }


    public void RegisterLogin()
    {
        using (SqlConnection conn = new SqlConnection(Globals.GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("PomCustomerRegisterLogin", conn))
            {
                try
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@pCustomerID", this.PomplingCustomerID);
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();                  
                }
                catch { ; }
            }
        }
    }
}